package com.yuqing.sentimentapp.home.presenter.impl;

import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.home.bean.NewsDataBean;
import com.yuqing.sentimentapp.home.fragment.NewsFragment;
import com.yuqing.sentimentapp.home.presenter.NewsPresenter;
import com.yuqing.sentimentapp.module.ModuleManager;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ShenHe on 2019/3/14.
 */

public class NewsPresenterImpl implements NewsPresenter {
    private NewsFragment newsFragment;

    public NewsPresenterImpl(NewsFragment newsFragment) {
        this.newsFragment = newsFragment;
    }

    @Override
    public void loadNewsData(String phone, String category, int page, int pageSize, boolean isRefresh) {
        ModuleManager.getInstance().loadNewsDataResult(newsFragment.getContext(), phone, category, page, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NewsDataBean>() {
                    @Override
                    protected void hideDialog() {
                        newsFragment.stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        newsFragment.startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(NewsDataBean bean) {
                        newsFragment.returnNewsResult(bean, isRefresh);
                    }

                    @Override
                    protected void onFailure(String message) {
                        newsFragment.onFailure(message);
                    }

                });
    }

    @Override
    public void loadSpecialTopicData(String phone, int page, int pageSize, boolean isRefresh) {
        ModuleManager.getInstance().loadSpecialTopicDataResult(newsFragment.getContext(), phone, page, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NewsDataBean>() {
                    @Override
                    protected void hideDialog() {
                        newsFragment.stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        newsFragment.startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(NewsDataBean bean) {
                        if (checkContext()) return;
                        newsFragment.returnNewsResult(bean, isRefresh);
                    }

                    @Override
                    protected void onFailure(String message) {
                        newsFragment.onFailure(message);
                    }

                });
    }

    @Override
    public void loadTopicAnalysisData(String phone, int page, int pageSize, boolean isRefresh) {
        ModuleManager.getInstance().loadTopicAnalysisDataResult(newsFragment.getContext(), phone, page, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NewsDataBean>() {
                    @Override
                    protected void hideDialog() {
                        newsFragment.stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        newsFragment.startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(NewsDataBean bean) {
                        if (checkContext()) return;
                        newsFragment.returnNewsResult(bean, isRefresh);
                    }

                    @Override
                    protected void onFailure(String message) {
                        newsFragment.onFailure(message);
                    }

                });
    }

    private boolean checkContext() {
        if (newsFragment.getActivity() == null || newsFragment.getActivity().isFinishing() || newsFragment.getActivity().isDestroyed())
            return true;
        return false;
    }
}
